/**
 * @param {number} x
 * @return {number}
 */
var mySqrt = function(x) {
    let left = 0
    let right = x

    while(left<=right){
        let middle = Math.floor(left+((right-left)/2))
        if(middle*middle===x) return middle

        if((middle*middle)>x){
            right = middle-1
        }else if((middle*middle)<x){
            let num = middle + 1
            if(num*num>x) return middle;
            left = num
        }
    }

    return -1
};